package org.example.component;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class TransactionManagerHandler {
    /**
     * 连接点：具体的方法
     * @param joinPoint
     */
    public void begin(JoinPoint joinPoint) {

        Object target = joinPoint.getTarget();//真是对象
        System.out.println("target = " + target);
        Object[] args = joinPoint.getArgs();
        System.out.println("args = " + Arrays.toString(args));

        System.out.println("开启事务");
    }

    public void commit() {
        System.out.println("提交事务");
    }

    public void rollback(Throwable ex) {
        System.out.println("回滚事务");
    }

    public void close() {
        System.out.println("关闭session");
    }


    //=================环绕通知===========================


    public void allInOne(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("=================环绕通知===========================");
        begin(joinPoint);
        try {
            //执行方法
            joinPoint.proceed();

            commit();
        } catch (Exception e) {
            rollback(e);
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }
}